package cn.tedu.tmall.admin.mall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/**
 * 自定义Redis配置，定制RedisTemplate对象
 */
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //1.创建操作redis的Java对象
        RedisTemplate<String,Serializable> redisTemplate = new RedisTemplate<>();
        //2.设置连接工厂L（访问redis也需要建立连接）
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //3.设置redistemplate对象存储数据时的序列化与反序列化方式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
